package leetcode.code0835;

import java.awt.Point;
import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	int n;

	public int largestOverlap(int[][] img1, int[][] img2) {
		n = img1.length;
		int ans1 = this.f1(img1, img2);// 移动img1左上角，枚举img1的左上角
		int ans2 = this.f1(img2, img1);// 移动img2左上角，枚举img2的左上角，相当于img1的右下角
		int ans3 = this.f2(img1, img2);
		int ans4 = this.f2(img2, img1);
		return Math.max(Math.max(ans1, ans2), Math.max(ans3, ans4));
	}

	private int f2(int[][] img1, int[][] img2) {
		int ans = 0;
		for (int x = 0; x < n; x++) {
			for (int y = 0; y < n; y++) {
				int sx1 = 0, sy1 = y, ex1 = n - x, ey1 = n;
				int cur = 0;
				for (int i1 = sx1, i2 = x; i1 < ex1; i1++, i2++) {
					for (int j1 = sy1, j2 = 0; j1 < ey1; j1++, j2++) {
						if (img1[i1][j1] == 1 && img2[i2][j2] == 1) {
							cur++;
						}
					}
				}
				ans = Math.max(ans, cur);
			}
		}
		return ans;
	}

	private int f1(int[][] img1, int[][] img2) {
		int ans = 0;
		for (int x = 0; x < n; x++) {
			for (int y = 0; y < n; y++) {
				int sx1 = 0, sy1 = 0, ex1 = n - x, ey1 = n - y;
				int cur = 0;
				for (int i1 = sx1, i2 = x; i1 < ex1; i1++, i2++) {
					for (int j1 = sy1, j2 = y; j1 < ey1; j1++, j2++) {
						if (img1[i1][j1] == 1 && img2[i2][j2] == 1) {
							cur++;
						}
					}
				}
				ans = Math.max(ans, cur);
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(1, this.largestOverlap(H.str2array1("[[0,1],[0,0]]"), H.str2array1("[[0,0],[1,0]]")));
		H.compare(1, this.largestOverlap(H.str2array1("[[0,0,0,0,1],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]"),
				H.str2array1("[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[1,0,0,0,0]]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		Point point = new Point(1, 1);
		Map<Point, Integer> map = new HashMap<>();
		map.put(point, 1);
		map.put(new Point(1, 2), 2);

		map.put(new Point(1, 2), map.get(new Point(1, 2)) + 1);
		System.out.println(map.get(new Point(1, 2)));
	}

}
